package com.msh.artascope.devcloud.service.api;

import com.msh.artascope.devcloud.client.po.ModuleRunningPo;
import com.msh.artascope.devcloud.client.qo.ModuleRunningQo;
import com.msh.artascope.devcloud.service.model.query.ModuleRunningQueryVo;
import com.msh.artascope.devcloud.service.model.vo.ModuleRunningVo;
import com.msh.artascope.devcloud.service.service.ModuleRunningService;
import com.msh.artascope.interfaces.IAccountGet;
import com.msh.artascope.interfaces.ILoginAccount;
import com.msh.frame.client.common.Result;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.client.validation.IDeleteVo;
import com.msh.frame.client.validation.IInsertVo;
import com.msh.frame.client.validation.IListVo;
import com.msh.frame.client.validation.IUpdateVo;
import com.msh.frame.web.base.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 运行中制品-api相关接口
 *
 * @author shihu
 * @email m-sh@qq.com
 */
@Tag(name = "运行中制品-api相关接口")
@Slf4j
@RestController
@RequestMapping("/api/moduleRunning")
public class ModuleRunningApi extends BaseController<ModuleRunningPo, ModuleRunningQo> {

  @Resource
  private ModuleRunningService moduleRunningService;

  @Resource
  private IAccountGet accountGet;

  /**
   * 运行中制品新增
   *
   * @param p
   * @return
   */
  @Operation(summary = "运行中制品新增")
  @PostMapping("insert")
  public Result<Long> insert(
      @Validated({IInsertVo.class, Default.class}) @RequestBody ModuleRunningVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    return Result.successReturn(moduleRunningService.insertVo(p));
  }

  /**
   * 运行中制品修改
   *
   * @param p
   * @return
   */
  @Operation(summary = "运行中制品修改")
  @PostMapping("update")
  public Result<Void> update(
      @Validated({IUpdateVo.class, Default.class}) @RequestBody ModuleRunningVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    moduleRunningService.update(p);
    return Result.voidReturn();
  }


  /**
   * 运行中制品删除
   *
   * @param p
   * @return
   */
  @Operation(summary = "运行中制品删除")
  @PostMapping("delete")
  public Result<Void> delete(
      @Validated({IDeleteVo.class, Default.class}) @RequestBody ModuleRunningPo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    moduleRunningService.deleteVo(p.getId(), loginAccount);
    return Result.voidReturn();
  }

  /**
   * 发布
   *
   * @param p
   * @return
   */
  @Operation(summary = "发布")
  @PostMapping("deploy")
  public Result<Void> deploy(
      @Validated({IUpdateVo.class, Default.class}) @RequestBody ModuleRunningPo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    moduleRunningService.releaseVo(p.getId(), loginAccount);
    return Result.voidReturn();
  }

  /**
   * 运行中制品详情
   *
   * @param id
   * @return
   */
  @Operation(summary = "运行中制品详情")
  @GetMapping("get")
  public Result<ModuleRunningVo> get(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleRunningService.getVo(id, loginAccount.getTenantId()));
  }

  /**
   * 运行中制品vo列表查询
   *
   * @param q
   * @return
   */
  @Operation(summary = "运行中制品vo列表查询")
  @PostMapping("list")
  public Result<List<ModuleRunningVo>> list(
      @Validated({IListVo.class}) @RequestBody ModuleRunningQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return Result.successReturn(moduleRunningService.listVo(q));
  }


  /**
   * 运行中制品vo分页查询
   *
   * @param q
   * @return
   */
  @Operation(summary = "运行中制品vo分页查询")
  @PostMapping("page")
  public Result<List<ModuleRunningVo>> page(
      @Validated({IListVo.class}) @RequestBody ModuleRunningQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return moduleRunningService.pageVo(q);
  }


  @Operation(summary = "设置副本数")
  @PostMapping("setSacle")
  public Result<Void> setSacle(
      @Validated({IUpdateVo.class, Default.class}) @RequestBody ModuleRunningVo vo) {
    BizException.doThrowIfNull("副本数不能为空", vo.getScale());
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    moduleRunningService.setScale(vo.getId(), vo.getScale(), loginAccount.getTenantId());
    return Result.voidReturn();
  }
}
